package com.aistar.controller;

import com.aistar.model.dto.job.LabelTypeAndTag;
import com.aistar.model.dto.job.RecruitmentAndCompanyDTO;
import com.aistar.model.entity.Company;
import com.aistar.model.entity.Employee;
import com.aistar.model.entity.Recruitment;
import com.aistar.service.job.*;
import com.aistar.util.BaiduAddressUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private IRecruitmentLabelService recruitmentLabelService;

    @Autowired
    private IRecruitmentService recruitmentService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ITagService tagService;

    @Autowired
    private IIndexService indexService;

    @GetMapping("/index")
    public String index(HttpServletRequest request, String name,String tagdt) {
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOList = new ArrayList<>();
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOList1 = new ArrayList<>();
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        if(employee!=null){
            List<Recruitment> recruitmentList = recruitmentService.search();
            for (Recruitment recruitment : recruitmentList) {
                RecruitmentAndCompanyDTO recruitmentAndCompanyDTO = new RecruitmentAndCompanyDTO();
                recruitmentAndCompanyDTO.setRecruitment(recruitment);
                Company company = companyService.findById(recruitment.getCompanyId());
                recruitmentAndCompanyDTO.setCompany(company);
                List<Integer> tagId = recruitmentLabelService.getTag(recruitment.getId());
                List<String> tagList = new ArrayList<>();
                for (Integer integer : tagId) {
                    String tag = tagService.searchDetails(integer);
                    tagList.add(tag);
                    recruitmentAndCompanyDTO.setTagList(tagList);
                }
                Integer count=recruitmentService.searchByCompanyId(company.getId()).size();
                recruitmentAndCompanyDTO.setCount(count);
                recruitmentAndCompanyDTOList.add(recruitmentAndCompanyDTO);
            }
            request.setAttribute("recruitment", recruitmentAndCompanyDTOList);
            List<Company> companyList = companyService.search();
            request.setAttribute("company", companyList);
            List<LabelTypeAndTag> labelTypeAndTagList = recruitmentService.tagIdByType();
            request.setAttribute("labeltypeandtag", labelTypeAndTagList);
            request.setAttribute("name", name);
            request.setAttribute("tagdt",tagdt);

            String ip = "58.210.179.66";
            BaiduAddressUtil addressUtil = new BaiduAddressUtil();
            String address = addressUtil.getAddress(ip);

            session.setAttribute("address",address);
            List<Integer> recruitmentLike=indexService.countDelivery(employee.getId());
            List<Recruitment> recruitmentList1=new ArrayList<>();
            for (Integer like : recruitmentLike) {
                Recruitment recruitment=recruitmentService.loadRecruitment(like);
                RecruitmentAndCompanyDTO recruitmentAndCompanyDTO=new RecruitmentAndCompanyDTO();
                recruitmentAndCompanyDTO.setRecruitment(recruitment);
                Company company = companyService.findById(recruitment.getCompanyId());
                recruitmentAndCompanyDTO.setCompany(company);
                List<Integer> tagId = recruitmentLabelService.getTag(recruitment.getId());
                List<String> tagList = new ArrayList<>();
                for (Integer integer : tagId) {
                    String tag = tagService.searchDetails(integer);
                    tagList.add(tag);
                    recruitmentAndCompanyDTO.setTagList(tagList);
                }
                Integer count=recruitmentService.searchByCompanyId(company.getId()).size();
                recruitmentAndCompanyDTO.setCount(count);
                recruitmentAndCompanyDTOList1.add(recruitmentAndCompanyDTO);
            }

            request.setAttribute("recruitment1",recruitmentAndCompanyDTOList1);
        }else{
            List<Recruitment> recruitmentList = recruitmentService.search();
            for (Recruitment recruitment : recruitmentList) {
                RecruitmentAndCompanyDTO recruitmentAndCompanyDTO = new RecruitmentAndCompanyDTO();
                recruitmentAndCompanyDTO.setRecruitment(recruitment);
                Company company = companyService.findById(recruitment.getCompanyId());
                recruitmentAndCompanyDTO.setCompany(company);
                List<Integer> tagId = recruitmentLabelService.getTag(recruitment.getId());
                List<String> tagList = new ArrayList<>();
                for (Integer integer : tagId) {
                    String tag = tagService.searchDetails(integer);
                    tagList.add(tag);
                    recruitmentAndCompanyDTO.setTagList(tagList);
                }
                Integer count=recruitmentService.searchByCompanyId(company.getId()).size();
                recruitmentAndCompanyDTO.setCount(count);
                recruitmentAndCompanyDTOList.add(recruitmentAndCompanyDTO);
            }
            request.setAttribute("recruitment", recruitmentAndCompanyDTOList);
            List<Company> companyList = companyService.search();
            request.setAttribute("company", companyList);
            List<LabelTypeAndTag> labelTypeAndTagList = recruitmentService.tagIdByType();
            request.setAttribute("labeltypeandtag", labelTypeAndTagList);
            request.setAttribute("name", name);
            request.setAttribute("tagdt",tagdt);

            String ip = "58.210.179.66";
            BaiduAddressUtil addressUtil = new BaiduAddressUtil();
            String address = addressUtil.getAddress(ip);
        }

        return "/WEB-INF/index.jsp";
    }
}
